package Export;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import weka.core.converters.CSVLoader;
import Definitions.GraphClass;
import Definitions.NodeClass;
import Global.ConstantVariable;
import Global.GlobalClass;

public class ExportClass {
	
	public static void exportToOtherFormats(int DataSet, GraphClass g, GlobalClass global)
	{
		
		
	}	
	
	//Delimeter Seperated Format
	//
	
	/*****************************************************************
	* Function Name:			convertContentFeaturesToDsvFormat   
	* Aim: 						To convert given content features to delimeter seperated files.  
	* Inputs:
	*  				
	* GraphClass graph:			defines which graph is used.
	* int contentType			defines the content type for whose attributes will be converted.
	* String path				full path of the destination
	* char delimiter			the delimiter character to be used for separation of the fields.
	* boolean includeHeader		include Attribute headers or not.
	* boolean includeNodeIDs	include Node orders or not.
	* boolean includeNodeNames  include Node names or not.
	* includeClassLabel			include Class labels or not.
	* boolean useClassOrders	use excact class names or class orders.
	* 
	* Outputs:
	* None 
	*  
	* Data Structures:
	* Graph
	* Node
	*  
	* Algorithms:
	*****************************************************************/
	
	public static void convertContentFeaturesToDsvFormat(GraphClass graph, GlobalClass global, int contentType, String path, char delimiter, boolean includeHeader, boolean includeNodeIDs, boolean includeNodeNames, boolean includeClassLabel, boolean useClassOrders)
	{
		FileWriter fstream;
		try	
		{
			fstream = new FileWriter(path);
			BufferedWriter outForDsvFormat = new BufferedWriter(fstream);
			
			ArrayList<NodeClass> datasetOfTheGraph = graph.getDataSet();
			int sizeOfTheDatasetOfTheGraph = datasetOfTheGraph.size();
			
			ArrayList<Double> attributeListOfTheNode = datasetOfTheGraph.get(0).getContentList().get(contentType).getAttributeList();
			int sizeOfTheAttributeList =  attributeListOfTheNode.size();
			
			if(includeHeader)
			{				
				if(includeNodeIDs)
				{
					outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.NODE_IDS_PREFIX + delimiter);
				}
				
				if(includeNodeNames)
				{
					outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.NODE_NAMES_PREFIX + delimiter);
				}
				
				for(int i=0; i<sizeOfTheAttributeList; i++)					
				{
					outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.ATTRIBUTE_NAMES_PREFIX + Integer.toString(i) + delimiter);
				}				
				outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.CLASS_NAMES_PREFIX + ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.LINE_END);
			}
			
			NodeClass currentNode;
						
			for(int i=0; i<sizeOfTheDatasetOfTheGraph; i++)
			{
				currentNode = datasetOfTheGraph.get(i);
				attributeListOfTheNode = currentNode.getContentList().get(contentType).getAttributeList();
				
				if(includeNodeIDs)
				{
					outForDsvFormat.write(Integer.toString(currentNode.getOrder()) + delimiter);
				}
				
				if(includeNodeNames)
				{
					outForDsvFormat.write(currentNode.getId()+ delimiter);
				}
				
				for(int j=0; j<attributeListOfTheNode.size(); j++)
					outForDsvFormat.write(Double.toString(attributeListOfTheNode.get(j))+delimiter);
				
				if(includeClassLabel)
				
				if(useClassOrders)
					outForDsvFormat.write(Integer.toString(currentNode.getOrder()));
				else
				{
					//System.out.println(GlobalClass.classList.get(currentNode.getClassOrder()).getName());					
					outForDsvFormat.write(global.classList.get(currentNode.getClassOrder()).getName());
				}
				
				outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.LINE_END);				
			}	
			
			outForDsvFormat.close();
		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//Delimeter Seperated Format
	//
	
	/*****************************************************************
	* Function Name:			convertContentFeaturesToDsvFormat   
	* Aim: 						To convert given content features to delimeter seperated files.  
	* Inputs:
	*  				
	* GraphClass graph:			defines which graph is used.
	* int contentType			defines the content type for whose attributes will be converted.
	* String path				full path of the destination
	* char delimiter			the delimiter character to be used for separation of the fields.
	* boolean includeHeader		include Attribute headers or not.
	* boolean includeNodeIDs	include Node orders or not.
	* boolean includeNodeNames  include Node names or not.
	* includeClassLabel			include Class labels or not.
	* boolean useClassOrders	use excact class names or class orders.
	* 
	* Outputs:
	* None 
	*  
	* Data Structures:
	* Graph
	* Node
	*  
	* Algorithms:
	*****************************************************************/
	
	public static void convertLinkFeaturesToDsvFormat(GraphClass graph, GlobalClass global, int contentType, String path, char delimiter, boolean includeHeader, boolean includeNodeIDs, boolean includeNodeNames, boolean includeClassLabel, boolean useClassOrders)
	{
		FileWriter fstream;
		try	
		{
			fstream = new FileWriter(path);
			BufferedWriter outForDsvFormat = new BufferedWriter(fstream);
			
			ArrayList<NodeClass> datasetOfTheGraph = graph.getDataSet();
			int sizeOfTheDatasetOfTheGraph = datasetOfTheGraph.size();
			
			ArrayList<Double> attributeListOfTheNode = datasetOfTheGraph.get(0).getContentList().get(contentType).getAttributeList();
			int sizeOfTheAttributeList =  attributeListOfTheNode.size();
			
			if(includeHeader)
			{				
				if(includeNodeIDs)
				{
					outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.NODE_IDS_PREFIX + delimiter);
				}
				
				if(includeNodeNames)
				{
					outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.NODE_NAMES_PREFIX + delimiter);
				}
				
				for(int i=0; i<sizeOfTheAttributeList; i++)					
				{
					outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.ATTRIBUTE_NAMES_PREFIX + Double.toString(i) + delimiter);
				}				
				outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.CLASS_NAMES_PREFIX + ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.LINE_END);
			}
			
			NodeClass currentNode;
						
			for(int i=0; i<sizeOfTheDatasetOfTheGraph; i++)
			{
				currentNode = datasetOfTheGraph.get(i);
				attributeListOfTheNode = currentNode.getContentList().get(contentType).getAttributeList();
				
				if(includeNodeIDs)
				{
					outForDsvFormat.write(Integer.toString(currentNode.getOrder()) + delimiter);
				}
				
				if(includeNodeNames)
				{
					outForDsvFormat.write(currentNode.getId()+ delimiter);
				}
				
				for(int j=0; j<attributeListOfTheNode.size(); j++)
					outForDsvFormat.write(Double.toString(attributeListOfTheNode.get(j))+delimiter);
				
				if(includeClassLabel)
				
				if(useClassOrders)
					outForDsvFormat.write(Integer.toString(currentNode.getOrder()));
				else
				{
					//System.out.println(GlobalClass.classList.get(currentNode.getClassOrder()).getName());					
					outForDsvFormat.write(global.classList.get(currentNode.getClassOrder()).getName());
				}
				
				outForDsvFormat.write(ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.LINE_END);				
			}	
			
			outForDsvFormat.close();
		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*****************************************************************
	* Function Name:				convertCSVFileToARFFFormat   
	* Aim: 							To convert CSV file to ARFF Format.  
	* Inputs:
	*  				
	* String sourceFilePath:		File path of the CSV file.
	* String destinationFilePath	File path of the ARFF file. 
	* 
	* Outputs:
	* None 
	*  
	* Data Structures:
	* Graph
	* Node
	*  
	* Algorithms:
	*****************************************************************/
	
	public static synchronized void convertCSVFileToARFFFormat(String sourceFilePath, String destinationFilePath)
	{
		
		try	
		{	
			//FileWriter outfstream;			
			//outfstream = new FileWriter(destinationFilePath);
			PrintStream ps = new PrintStream(destinationFilePath);			
			String[] options=weka.core.Utils.splitOptions(sourceFilePath);
			PrintStream oldPs = System.out; //backup
			System.setOut(ps);			
			CSVLoader.main(options);			
			System.setOut(oldPs);
		} 
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
